/**
 * 派单处理js模块(支站和中转站公用)
 */
//获取所有等待的派单的订单列表
function getToSenddOrders(){
	$(".td_data").remove();
	$.getJSON("../order_queryToSendOrders.json",function(data){
        if(data.success && data.orders != null){
            $.each(data.orders,function(index,item){
                    $(".table-orders-toSend").append("<tr class='td_data'>" +
						"<td>"+(item.isQuick?"加急件":"")+"</td>" +
						"<td>"+item.orderNo+ "</td><td>"
                        +item.city+item.area+item.street+item.address+"</td><td>"
                        +item.user.stationName+"</td><td>"
                        +" <button  class='btn btn-primary btn-sm btn-table' onclick=\"isSend("+"'receive','"+item.orderNo+"')"+"\">派送成功</button >"+"&nbsp"+"<button class='btn btn-primary btn-sm btn-table' data-target='#errorMessage' data-toggle='modal' onclick=\"showSendFailLay('"+item.orderNo+"')\">派送失败</button>"+"</td><td>"
                        +(item.deliverMessage==1?'用户不在家':'')+"</td>"
                        +"</tr>");
            });
        }
    });
}
//派送
function  isSend(type,order,middleMessage){
	if(type=="receive"){
		$.getJSON("../order_dealSend.json",{type : type,order: order,middleMessage:middleMessage},function(data){
			if(data.success){
                // getCounterForSendLetter();
                // getQuickerForSendLetter();
				getToSenddOrders();
				window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
			}
		});
	}
}
function showSendFailLay(orderNo){
	$(".btn-save-error").unbind("click");
	$(".btn-save-error").click(function(){
		var failMessageId=$(".select-error-info  option:selected").val();
		if(failMessageId!=null){
	    	$.getJSON("../order_dealSend.json",{type : "sendBack",order: orderNo,failMessageId:failMessageId},function(data){
	    		if(data.success){
	    			window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
                    $(".select-error-info").val("");
                    $(".close").trigger("click");
                    // getCounterForSendLetter();
                    // getQuickerForSendLetter();
                    getToSenddOrders();
	    			
	    		}
	    	});
		}
		else{
            window.wxc.xcConfirm("请选择失败理由!", window.wxc.xcConfirm.typeEnum.warning);
		}
	})
	
}


$(document).ready(function () {
		getToSenddOrders();
})

